package com.tfswue.lhj.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.tfswue.lhj.entity.User;
import com.tfswue.lhj.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@Slf4j
@RestController
@RequestMapping("/api/users")
@Tag(name="用户接口",description = "符合RESTful风格的用户管理接口")
public class RestFulUserController {
    @Autowired
    private UserService userService;
    //查询所有用户
    @Operation(summary = "查询所有用户", description = "获取系统中所有用户的列表")
    @GetMapping
    @ApiResponse(responseCode = "200", description = "成功获取用户列表")
    public List<User> getAllUsers(){
        return userService.list();
    }
    // 添加构造方法以验证控制器是否被加载
    public RestFulUserController() {
        log.info("RestFulUserController 已初始化");
    }
    //查询单个用户
    @Cacheable(value = "dataCache", key="#id")
    @GetMapping("/{id}/{name}")
    public User getUserById(@PathVariable("id") Integer id,
                            @PathVariable("name") String name) {
        log.error("查询用户ID: {}, 名称: {}", id, name);
        return userService.getById(id);
    }
    @GetMapping("/{id}")
    public User getUserById(@PathVariable("id") Integer id) {
        log.error("仅使用ID查询用户: {}", id);
        return userService.getById(id);
    }
    //分页查询，page和size分别表示页码和每页大小
    @GetMapping("/querypage/{page}/{size}")
    public List<User> queryPage(@PathVariable("page") Integer page,
                                    @PathVariable("size") Integer size) {
        Page<User> testpage = new Page<>(page, size);
        Page<User> userPage = userService.page(testpage, null);
        return userPage.getRecords();
    }
    //多条件分页查询
    @GetMapping("/querylist/{page}/{size}")
    public List<User> queryPageByName(@PathVariable("page") Integer page,
                                      @PathVariable("size") Integer size,
                                      @RequestBody User user) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        // 添加查询条件
        if (user.getUsername() != null) {
            queryWrapper.like("username", user.getUsername());
        }
        if (user.getEmail() != null) {
            queryWrapper.eq("email", user.getEmail());
        }
        Page<User> testpage = new Page<>(page, size);
        // 使用 Lambda 查询条件
        Page<User> userPage = userService.page(testpage, queryWrapper);
        return userPage.getRecords();
    }
    //添加用户
    @PostMapping
    public boolean addUser(@RequestBody User user) {
        return  userService.save(user);

    }
    //修改用户
    @CachePut(value = "dataCache", key = "#user.id")
    @PutMapping
    public boolean updateUser(@RequestBody User user) {

        return userService.updateById(user);
    }
    //删除用户
    @CacheEvict(value = "dataCache", key = "#id")
    @DeleteMapping("/{id}")
    public boolean deleteUser(@PathVariable("id") Integer id) {
        return userService.removeById(id);
    }
}
